import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ichinesereader/controller/schooladmin.dart';
import 'package:ichinesereader/pages/teacher/addStudentsToClass.dart';
import 'package:ichinesereader/routes/routePages.dart';
import 'package:ichinesereader/utils/log.dart';
import 'package:ichinesereader/widgets/buttons/floatingActionStudentManagement.dart';
import 'package:ichinesereader/widgets/grids/studentsDatagrid.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:ichinesereader/constants.dart';
import 'package:ichinesereader/controller/cache.dart';
import 'package:ichinesereader/widgets/layouts/appdrawerTeacher.dart';
import 'package:ichinesereader/widgets/layouts/customAppBar.dart';
import '../../themes/pageStyles.dart';
import '../../widgets/sidebars/teacherSidebar.dart';

class StudentsManagement extends StatefulWidget {
  static const String routeName = '/teacher/students';

  @override
  State<StudentsManagement> createState() => _StudentsManagementState();
}

class _StudentsManagementState extends State<StudentsManagement> {
  final SchoolAdminController controller = Get.find();
  final box = Hive.box('appconfig');
  bool isTeacher = false;

  @override
  void initState() {
    isTeacher = box.get(CacheManagerKey['nanhairole'].toString()) ==
        Constants.NanhaiRole_Teacher;
    String _classId = box.get(CacheManagerKey['currentclass'].toString());

    if (isTeacher) {
      Future.delayed(Duration(milliseconds: 100), () {
        controller.fetchSchoolStudentsByClassId(_classId, true);
      });
    }
    super.initState();
  }

  @override
  void dispose() {
    controller.studentItems.clear();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    Log.debug('build teacher StudentsManagement');
    final isSmallScreen =
        MediaQuery.of(context).size.width < Constants.DefinedScreenSmallSize;
    final isBigScreen =
        MediaQuery.of(context).size.width > Constants.DefinedScreenBigSize;
    int _selectedSidebarItem = RoutePages.teacherPageItems
        .indexWhere((page) => page.route == Get.currentRoute);
    final PageStyles _pageStyles = PageStyles();
    //final UserController _userController = Get.find();

    return Scaffold(
      appBar: isSmallScreen ? CustomAppBar(title: 'Students'.tr) : null,
      drawer: isSmallScreen ? TeacherAppDrawer() : null,
      floatingActionButton: FloatingActionButtonStudentManagement(),
      body: Container(
        decoration: _pageStyles.pageBoxDecoration(),
        child: Row(
          children: [
            Visibility(
                visible: !isSmallScreen,
                child: TeacherSidebarX(
                  selectedIndex: _selectedSidebarItem,
                  isBigScreen: isBigScreen,
                )),
            Expanded(
              child: Container(
                margin: _pageStyles.mainContainerWithPaginationEdgeInsets(),
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Visibility(
                      visible: isTeacher,
                      child: controller.obx(
                        (data) => Expanded(
                            child: StudentsDataGrids(
                                false, controller.studentItems, false)),
                        onEmpty: TextButton(
                          child: Text(
                            'Add Students'.tr,
                            style: TextStyle(
                                fontFamily: 'Montserrat',
                                color: Get.isDarkMode
                                    ? Get.theme.primaryColorDark
                                    : Get.theme.primaryColorLight),
                          ),
                          onPressed: () {
                            Get.to(() => AddStudentsToClassPage());
                          },
                        ),
                        onLoading: Container(),
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
